function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

/**
 * 封装微信requert
 */
function request (url, data = {}, method = 'POST', header = '') {
  wx.showLoading({
    title: '加载中...'
  })
  return new Promise(function (resolve, reject) {
    wx.request({
      url: url,
      data: data,
      method: method,
      header: {
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        wx.hideLoading()
        if (res.statusCode === 200) {
          if (res.data.code === 40101) {
          } else if (res.data.code === 40102) {
            // wx.showModal({
            //   title: '温馨提示',
            //   content: res.data.msg,
            //   success (res) {
            //     if (res.confirm) {
            //     } else {
            //       console.log('失败')
            //     }
            //   }
            // })
          } else if (res.data.code === 500) {
            wx.showModal({
              title: '温馨提示',
              content: res.data.msg
              // success (res) {
              //   if (res.confirm) {
              //     wx.navigateBack({
              //       delta: -1
              //     })
              //   }
              // }
            })
          } else {
            resolve(res.data)
          }
        } else {
          reject(res.errMsg)
        }
      },
      fail: function (err) {
        reject(err)
      }
    }
    )
  })
}

export default {
  formatNumber,
  formatTime,
  request
}
